package com.embhealth.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.embhealth.constant.MessageConstant;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.entity.Result;
import com.embhealth.pojo.Food;
import com.embhealth.pojo.Sport;
import com.embhealth.service.FoodService;
import com.embhealth.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: chuanzhi_helath
 * @description: 膳食管理
 * @author: xu jiangdong
 * @DateTime: 2020-08-21 15:54
 **/
@RestController
@RequestMapping("/food")
public class FoodController {

    @Reference
    private FoodService foodService;

    /**
     * 上传膳食管理数据
     *
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<Food> foods = new ArrayList<>();
            for (String[] strings : list) {
                Food food = new Food();
                food.setSuitableAge("");
                food.setFoodID(Integer.parseInt(strings[0]));
                food.setFoodParentID(strings[1]);
                food.setFoodName(strings[2]);
                food.setFoodtype(strings[3]);
                food.setFoodTip(strings[4]);
                food.setFoodEated(strings[5]);
                food.setFoodWater(strings[6]);
                food.setFoodKcal(strings[7]);
                food.setFoodKj(strings[8]);
                food.setFoodProtein(strings[9]);
                food.setFoodFat(strings[10]);
                food.setFoodCho(strings[11]);
                food.setFoodWeight(strings[12]);
                food.setGxy(strings[13]);
                food.setTnb(strings[14]);
                food.setGxb(strings[15]);
                food.setNcz(strings[16]);
                food.setXnxgjb(strings[17]);
                food.setXzyc(strings[18]);
                food.setFp(strings[19]);
                food.setTf(strings[20]);
                food.setTnbsb(strings[21]);
                food.setUnitBulk(strings[23]);
                food.setRegionID(strings[24]);
                food.setGdjb(strings[25]);
                food.setFoodUnit(strings[22]);
                foods.add(food);
            }
            foodService.handleAdd(foods);
            return new Result(true, MessageConstant.IMPORT_FOOD_SUCCESS);
        } catch (IOException    e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_FOOD_FAIL);
        }
    }

    /**
     * 分页查询
     *
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return foodService.findPage(queryPageBean);
    }

    /**
     * 添加单项膳食管理数据记录
     *
     * @param food
     * @return
     */
    @RequestMapping("/handleAdd")
    public Result handleAdd(@RequestBody Food food) {
        try {
            foodService.handleAdd(food);
            return new Result(true, MessageConstant.ADD_FOOD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_FOOD_SUCCESS);
        }
    }
}
